package com.wertop.viid.common.exception;

import com.wertop.viid.common.result.ConfirmStatusType;
import com.wertop.viid.common.result.ResponseStatusObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 异常处理器
 * @Author yempty
 * @Date 2020/5/14 9:16
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ValidateException.class)
    public ResponseStatusObject handleRenException(ValidateException ex) {
        return ResponseStatusObject.create(null, ConfirmStatusType.Validate_Error.getCode(), ex.getMsg());
    }

    @ExceptionHandler(GlobalException.class)
    public ResponseStatusObject handleRenException(GlobalException ex) {
        return ResponseStatusObject.create(ConfirmStatusType.OtherError);
    }

    @ExceptionHandler(HttpMessageConversionException.class)
    public ResponseStatusObject handHttpMessageConversionException(HttpMessageConversionException ex) {
        ex.printStackTrace();
        return ResponseStatusObject.create(ConfirmStatusType.Invalid_JSON_Format);
    }

    @ExceptionHandler(Exception.class)
    public ResponseStatusObject handleException(Exception ex) {
        log.error(ex.getMessage(), ex);

        saveLog(ex);

        return ResponseStatusObject.create(ConfirmStatusType.OtherError);
    }

    /**
     * 保存异常日志
     */
    private void saveLog(Exception ex) {

    }
}
